package com.haoxi.gateway.test.message;

import com.haoxi.gateway.test.SGIPConstant;
import com.haoxi.gateway.test.util.TypeConvert;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class SGIPSubmitMessage extends SGIPMessage {

    private String outStr;
    private String SPNumber;
    String ChargeNumber;
    String UserNumber[];
    String CorpId;
    String ServiceType;
    int FeeType;
    String FeeValue;
    String GivenValue;
    int AgentFlag;
    int MorelatetoMTFlag;
    int Priority;
    Date ExpireTime;
    Date ScheduleTime;
    int ReportFlag;
    int TP_pid;
    int TP_udhi;
    int MessageCoding;
    int MessageType;
    int MessageLen;
    byte MessageContent[];
    String reserve;
    int userCount;

    public SGIPSubmitMessage(byte[] buf) throws IllegalArgumentException {
        super.buf = new byte[buf.length];
        System.arraycopy(buf, 0, super.buf, 0, buf.length);
        // 消息头
        message_Length = TypeConvert.byte2int(super.buf, 0);
        command_ID = TypeConvert.byte2int(super.buf, 4);
        src_node_Id = TypeConvert.byte2int(super.buf, 8);
        time_Stamp = TypeConvert.byte2int(super.buf, 12);
        sequence_Id = TypeConvert.byte2int(super.buf, 16);

    }

    public String getSPNumber() {
        byte tmpId[] = new byte[21];
        System.arraycopy(buf, 20, tmpId, 0, 21);
        String tmpStr = (new String(tmpId)).trim();
        if (tmpStr.indexOf(0) >= 0)
            SPNumber = tmpStr.substring(0, tmpStr.indexOf(0));
        else
            SPNumber = tmpStr;
        return SPNumber;
    }

    public String getChargeNumber() {
        byte tmpId[] = new byte[21];
        System.arraycopy(buf, 41, tmpId, 0, 21);
        String tmpStr = (new String(tmpId)).trim();
        if (tmpStr.indexOf(0) >= 0) {
            ChargeNumber = tmpStr.substring(0, tmpStr.indexOf(0));
        } else {
            ChargeNumber = tmpStr;
        }
        return ChargeNumber;
    }

    public int getUserCount() {
        userCount = buf[62];
        return userCount;
    }

    public String[] getUserNumber() {
        int userCound = this.getUserCount();
        String[] result = new String[userCound];
        int i = 0;
        byte[] tmp = new byte[21];
        for (i = 0; i < userCound; i++) {
            System.arraycopy(buf, 63 + i * 21, tmp, 0, 21);
            String tmpStr = (new String(tmp)).trim();
            if (tmpStr.indexOf(0) >= 0) {
                result[i] = tmpStr.substring(0, tmpStr.indexOf(0));
            } else {
                result[i] = tmpStr;
            }

        }
        return result;
    }

    public String getCorpId() {
        int loc = 63 + getUserCount() * 21;
        byte tmpId[] = new byte[5];
        System.arraycopy(buf, loc, tmpId, 0, 5);
        String tmpStr = (new String(tmpId)).trim();
        if (tmpStr.indexOf(0) >= 0) {
            CorpId = tmpStr.substring(0, tmpStr.indexOf(0));
        } else {
            CorpId = tmpStr;
        }
        return CorpId;
    }

    public String getServiceType() {
        int loc = 68 + getUserCount() * 21;
        byte tmpId[] = new byte[10];
        System.arraycopy(buf, loc, tmpId, 0, 10);
        String tmpStr = (new String(tmpId)).trim();
        if (tmpStr.indexOf(0) >= 0) {
            ServiceType = tmpStr.substring(0, tmpStr.indexOf(0));
        } else {
            ServiceType = tmpStr;
        }
        return ServiceType;
    }

    public int getFeeType() {
        int loc = 78 + getUserCount() * 21;
        this.FeeType = buf[loc];
        return FeeType;
    }

    public String getFeeValue() {
        int loc = 79 + getUserCount() * 21;
        byte tmpId[] = new byte[6];
        System.arraycopy(buf, loc, tmpId, 0, 6);
        String tmpStr = (new String(tmpId)).trim();
        if (tmpStr.indexOf(0) >= 0) {
            FeeValue = tmpStr.substring(0, tmpStr.indexOf(0));
        } else {
            FeeValue = tmpStr;
        }
        return FeeValue;
    }

    public String getGivenValue() {
        int loc = 85 + getUserCount() * 21;
        byte tmpId[] = new byte[6];
        System.arraycopy(buf, loc, tmpId, 0, 6);
        String tmpStr = (new String(tmpId)).trim();
        if (tmpStr.indexOf(0) >= 0) {
            GivenValue = tmpStr.substring(0, tmpStr.indexOf(0));
        } else {
            GivenValue = tmpStr;
        }
        return GivenValue;
    }

    public int getAgentFlag() {
        int loc = 91 + getUserCount() * 21;
        AgentFlag = buf[loc];
        return AgentFlag;
    }

    public int getMorelatetoMTFlag() {
        int loc = 92 + getUserCount() * 21;
        MorelatetoMTFlag = buf[loc];
        return MorelatetoMTFlag;
    }

    public int getPriority() {
        int loc = 93 + getUserCount() * 21;
        Priority = buf[loc];
        return Priority;
    }

    public String getExpireTimeStr() {
        int loc = 94 + getUserCount() * 21;
        byte tmpId[] = new byte[16];
        System.arraycopy(buf, loc, tmpId, 0, 16);
        String tmpTime = (new String(tmpId)).trim();
        if (tmpTime.indexOf(0) >= 0) {
            tmpTime = tmpTime.substring(0, tmpTime.indexOf(0));
        }
        return tmpTime;
    }

    public Date getExpireTime() {
        int loc = 94 + getUserCount() * 21;
        byte tmpId[] = new byte[16];
        System.arraycopy(buf, loc, tmpId, 0, 16);
        String tmpstr = new String(tmpId).trim();
        if (!"".equals(tmpstr)) {
            byte tmpbyte[] = new byte[2];
            System.arraycopy(tmpId, 0, tmpbyte, 0, 2);
            String temp = new String(tmpbyte);
            int year = Integer.parseInt(temp);
            System.arraycopy(tmpId, 2, tmpbyte, 0, 2);
            temp = new String(tmpbyte);
            int mm = Integer.parseInt(temp);
            System.arraycopy(tmpId, 4, tmpbyte, 0, 2);
            temp = new String(tmpbyte);
            int dd = Integer.parseInt(temp);
            System.arraycopy(tmpId, 6, tmpbyte, 0, 2);
            temp = new String(tmpbyte);
            int hh = Integer.parseInt(temp);
            System.arraycopy(tmpId, 8, tmpbyte, 0, 2);
            temp = new String(tmpbyte);
            int min = Integer.parseInt(temp);
            System.arraycopy(tmpId, 10, tmpbyte, 0, 2);
            temp = new String(tmpbyte);
            int ss = Integer.parseInt(temp);
            Calendar calendar = Calendar.getInstance();
            calendar.set(year, mm - 1, dd, hh, min, ss);
            ExpireTime = calendar.getTime();
        }
        return ExpireTime;
    }

    public Date getScheduleTime() {
        int loc = 110 + getUserCount() * 21;
        byte tmpId[] = new byte[16];
        System.arraycopy(buf, loc, tmpId, 0, 16);
        String tmpstr = new String(tmpId).trim();
        if (!"".equals(tmpstr)) {
            byte tmpbyte[] = new byte[2];
            System.arraycopy(tmpId, 0, tmpbyte, 0, 2);
            String temp = new String(tmpbyte);
            int year = Integer.parseInt(temp);
            System.arraycopy(tmpId, 2, tmpbyte, 0, 2);
            temp = new String(tmpbyte);
            int mm = Integer.parseInt(temp);
            System.arraycopy(tmpId, 4, tmpbyte, 0, 2);
            temp = new String(tmpbyte);
            int dd = Integer.parseInt(temp);
            System.arraycopy(tmpId, 6, tmpbyte, 0, 2);
            temp = new String(tmpbyte);
            int hh = Integer.parseInt(temp);
            System.arraycopy(tmpId, 8, tmpbyte, 0, 2);
            temp = new String(tmpbyte);
            int min = Integer.parseInt(temp);
            System.arraycopy(tmpId, 10, tmpbyte, 0, 2);
            temp = new String(tmpbyte);
            int ss = Integer.parseInt(temp);
            Calendar calendar = Calendar.getInstance();
            calendar.set(year, mm - 1, dd, hh, min, ss);
            ScheduleTime = calendar.getTime();
        }
        return ScheduleTime;
    }

    public String getScheduleTimeStr() {
        int loc = 110 + getUserCount() * 21;
        byte tmpId[] = new byte[16];
        System.arraycopy(buf, loc, tmpId, 0, 16);
        String tmpTime = (new String(tmpId)).trim();
        if (tmpTime.indexOf(0) >= 0) {
            tmpTime = tmpTime.substring(0, tmpTime.indexOf(0));
        }
        return tmpTime;
    }

    public int getReportFlag() {
        int loc = 126 + getUserCount() * 21;
        ReportFlag = buf[loc];
        return ReportFlag;
    }

    public int getTP_pid() {
        int loc = 127 + getUserCount() * 21;
        TP_pid = buf[loc];
        return TP_pid;
    }

    public int getTP_udhi() {
        int loc = 128 + getUserCount() * 21;
        TP_udhi = buf[loc];
        return TP_udhi;
    }

    public int getMessageCoding() {
        int loc = 129 + getUserCount() * 21;
        MessageCoding = buf[loc];
        return MessageCoding;
    }

    public int getMessageType() {
        int loc = 130 + getUserCount() * 21;
        MessageType = buf[loc];
        return MessageType;
    }

    public int getMessageLength() {
        int loc = 131 + getUserCount() * 21;
        MessageLen = TypeConvert.byte2int(super.buf, loc);
        return MessageLen;
    }

    public byte[] getMessageContent() {
        int loc = 135 + getUserCount() * 21;
        MessageContent = new byte[getMessageLength()];
        System.arraycopy(buf, loc, MessageContent, 0, getMessageLength());
        return MessageContent;

    }

    public String getReserve() {
        int loc = 135 + getUserCount() * 21 + getMessageLength();
        byte tmpId[] = new byte[8];
        System.arraycopy(buf, loc, tmpId, 0, 8);
        String tmpStr = (new String(tmpId)).trim();
        if (tmpStr.indexOf(0) >= 0) {
            reserve = tmpStr.substring(0, tmpStr.indexOf(0));
        } else {
            reserve = tmpStr;
        }
        return reserve;

    }

    public byte[] getsubmitSequenceNumber() {
        byte[] tmp = new byte[12];
        System.arraycopy(buf, 8, tmp, 0, 12);
        return tmp;
    }

    public String toString() {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyMMddHHmmss");
        String tmpStr = "SGIP_Submit: ";
        tmpStr = String.valueOf(String.valueOf((new StringBuffer(String
                .valueOf(String.valueOf(tmpStr))))
                .append("\r\nmessage_Length=").append(message_Length)));
        tmpStr = String.valueOf(String.valueOf((new StringBuffer(String
                .valueOf(String.valueOf(tmpStr)))).append("\r\ncommand_ID=")
                .append(command_ID)));
        tmpStr = String.valueOf(String.valueOf((new StringBuffer(String
                .valueOf(String.valueOf(tmpStr)))).append("\r\nsrc_node_Id=")
                .append(src_node_Id)));
        tmpStr = String.valueOf(String.valueOf((new StringBuffer(String
                .valueOf(String.valueOf(tmpStr)))).append("\r\ntime_Stamp=")
                .append(time_Stamp)));

        tmpStr = String.valueOf(String.valueOf((new StringBuffer(String
                .valueOf(String.valueOf(tmpStr)))).append("\r\nSequence_Id=")
                .append(getSequenceId())));
        outStr = "\r\nSPNumber=".concat(String.valueOf(String
                .valueOf(getSPNumber())));
        outStr = String.valueOf(String.valueOf((new StringBuffer(String
                .valueOf(String.valueOf(outStr)))).append("\r\nChargeNumber=")
                .append(getChargeNumber())));
        outStr = String.valueOf(String.valueOf((new StringBuffer(String
                .valueOf(String.valueOf(outStr)))).append("\r\nUserCount=")
                .append(getUserCount())));
        String[] userNumber = getUserNumber();
        for (int t = 0; t < getUserCount(); t++) {
            outStr = String.valueOf(String.valueOf((new StringBuffer(String
                    .valueOf(String.valueOf(outStr))))
                    .append("\r\nUserNumber[").append(t).append("]=").append(
                            userNumber[t])));
        }
        outStr = String.valueOf(String.valueOf((new StringBuffer(String
                .valueOf(String.valueOf(outStr)))).append("\r\nCorpId=")
                .append(getCorpId())));
        outStr = String.valueOf(String.valueOf((new StringBuffer(String
                .valueOf(String.valueOf(outStr)))).append("\r\nServiceType=")
                .append(this.getServiceType())));
        outStr = String.valueOf(String.valueOf((new StringBuffer(String
                .valueOf(String.valueOf(outStr)))).append("\r\nFeeType=")
                .append(this.getFeeType())));
        outStr = String.valueOf(String.valueOf((new StringBuffer(String
                .valueOf(String.valueOf(outStr)))).append("\r\nFeeValue=")
                .append(this.getFeeValue())));
        outStr = String.valueOf(String.valueOf((new StringBuffer(String
                .valueOf(String.valueOf(outStr)))).append("\r\nGivenValue=")
                .append(this.getGivenValue())));
        outStr = String.valueOf(String.valueOf((new StringBuffer(String
                .valueOf(String.valueOf(outStr)))).append("\r\nAgentFlag=")
                .append(this.getAgentFlag())));
        outStr = String.valueOf(String.valueOf((new StringBuffer(String
                .valueOf(String.valueOf(outStr)))).append(
                "\r\nMorelatetoMTFlag=").append(this.getMorelatetoMTFlag())));
        outStr = String.valueOf(String.valueOf((new StringBuffer(String
                .valueOf(String.valueOf(outStr)))).append("\r\nPriority=")
                .append(this.getPriority())));
        outStr = String.valueOf(String.valueOf((new StringBuffer(String
                .valueOf(String.valueOf(outStr)))).append("\r\nExpireTime=")
                .append(this.getExpireTime())));
        outStr = String.valueOf(String.valueOf((new StringBuffer(String
                .valueOf(String.valueOf(outStr)))).append("\r\nScheduleTime=")
                .append(this.getScheduleTime())));
        outStr = String.valueOf(String.valueOf((new StringBuffer(String
                .valueOf(String.valueOf(outStr)))).append("\r\nReportFlag=")
                .append(this.getReportFlag())));
        if (getExpireTime() != null)
            outStr = String.valueOf(String.valueOf((new StringBuffer(String
                    .valueOf(String.valueOf(outStr))))
                    .append("\r\nExpireTime=").append(
                            dateFormat.format(getExpireTime()))));
        else
            outStr = String.valueOf(String.valueOf(outStr)).concat(
                    "\r\nExpireTime=null");
        if (this.getScheduleTime() != null)
            outStr = String.valueOf(String.valueOf((new StringBuffer(String
                    .valueOf(String.valueOf(outStr)))).append(
                    "\r\nScheduleTime=").append(
                    dateFormat.format(getScheduleTime()))));
        else
            outStr = String.valueOf(String.valueOf(outStr)).concat(
                    "\r\nScheduleTime=null");
        outStr = String.valueOf(String.valueOf((new StringBuffer(String
                .valueOf(String.valueOf(outStr)))).append("\r\nReportFlag=")
                .append(this.getReportFlag())));
        outStr = String.valueOf(String.valueOf((new StringBuffer(String
                .valueOf(String.valueOf(outStr)))).append("\r\nTP_pid=")
                .append(this.getTP_pid())));
        outStr = String.valueOf(String.valueOf((new StringBuffer(String
                .valueOf(String.valueOf(outStr)))).append("\r\nTP_udhi=")
                .append(getTP_udhi())));
        outStr = String.valueOf(String.valueOf((new StringBuffer(String
                .valueOf(String.valueOf(outStr)))).append("\r\nMessageCoding=")
                .append(getMessageCoding())));
        outStr = String.valueOf(String.valueOf((new StringBuffer(String
                .valueOf(String.valueOf(outStr)))).append("\r\nMessageType=")
                .append(getMessageType())));
        outStr = String.valueOf(String.valueOf((new StringBuffer(String
                .valueOf(String.valueOf(outStr)))).append("\r\nMessageLength=")
                .append(this.getMessageLength())));
        outStr = String.valueOf(String.valueOf((new StringBuffer(String
                .valueOf(String.valueOf(outStr))))
                .append("\r\nMessageContent=").append(
                        new String(this.getMessageContent()))));
        outStr = String.valueOf(String.valueOf((new StringBuffer(String
                .valueOf(String.valueOf(outStr)))).append("\r\nreserve=")
                .append(this.getReserve())));
        tmpStr = String.valueOf(tmpStr) + String.valueOf(outStr);
        return tmpStr;
    }

    public int getCommandId() {
        return 3;
    }
}
